package com.olc.railing.camera;

import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.olc.railing.base.BaseActivity;
import com.olc.railing.base.BaseApplication;
import com.olc.railing.utils.CommUtils;
import com.olc.railing.utils.FileUtils;
import com.olc.railing.utils.MyLog;
import com.olc.railing.utils.TimeUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class VideoRecordManager {
    private static final String TAG = VideoRecordManager.class.getSimpleName();
    private VideoRecord videoRecord;
    private MediaRecorder mMediaRecorder;
    private String vehiclePalteNo ="陕A12344";
    private   File mediaFile ;//视频存储目录

    public VideoRecordManager(VideoRecord videoRecord,String vehiclePalteNo) {
        this.videoRecord = videoRecord;
        this.vehiclePalteNo = vehiclePalteNo;
    }
    public void initRecord(){
        mediaFile = new File(getMediaFilePath());
        if (!mediaFile.exists()) {
            mediaFile.mkdirs();
        }

        File saveFile = new File(BaseApplication.getDestinationFileDir());
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
    }


    /*
     * 相机预览前的准备工作代码 单独抽出来
     * */
    public boolean prepareVideoRecorder() {
        MyLog.d(MyLog.TAG,"prepareVideoRecorder ");
        if(mMediaRecorder ==null){
            mMediaRecorder = new MediaRecorder();
        }

        //mMediaRecorder.reset();
        /*camera相关设置部分*/
        try {
            videoRecord.getCamera().unlock();
        } catch (Exception e) {
            e.printStackTrace();
            MyLog.d(MyLog.TAG," unlock = "+e.toString());
            return false;
        }

        mMediaRecorder.setCamera( videoRecord.getCamera());
        /*recorder设置部分*/
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));
        mMediaRecorder.setPreviewDisplay(videoRecord.getSurfaceHolder().getSurface());
        mMediaRecorder.setOutputFile(getOutputMediaFile());
        try {
            mMediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            MyLog.d(MyLog.TAG," prepare = "+e.toString());
            return false;
        }
        return true;
    }
    public void startRecord(){
        boolean isPrepare  = prepareVideoRecorder();
        MyLog.d(MyLog.TAG,"准备录像 isPrepare = "+isPrepare);
        if(isPrepare){
            mMediaRecorder.start();
            MyLog.d(MyLog.TAG,"开始录像");
        }

    }

    public String getMediaFilePath(){
        return BaseApplication.getSourceFileDir() +TimeUtils.getTime("yyyyMMddHHmm",System.currentTimeMillis()+"")+"_"+vehiclePalteNo;
    }
    public String getSorceFilePath(){
        return  mediaFile.getAbsolutePath();
    }


    public String getDestinationFileName(){
        return  TimeUtils.getTime("yyyyMMddHHmm",System.currentTimeMillis()+"")+"_"+vehiclePalteNo +".zip";
    }
    /*
     * 获取录制视频的日期 作为存储文件路径一部分
     * */
    /*
     *创建视频存储文件夹 录制好的视频存储在手机外部存储中 以录像时间+mp4格式命名
     * */
    private String getOutputMediaFile() {
       // MyLog.d(MyLog.TAG, "获取视频存储的位置 ");
        String result =  mediaFile.getAbsolutePath() + File.separator + TimeUtils.getTime("yyyyMMddHHmmss",System.currentTimeMillis()+"")+".mp4";

        File file = new File(result);
     //   MyLog.d(MyLog.TAG, "获取视频存储的位置 result = "+result+" file.exist "+file.exists());
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MyLog.d(MyLog.TAG, "获取视频存储的位置 result = "+result+" file.exist "+file.exists());
        return result;
    }
    /**
     * 停止录制
     */

    public void stop() {

        if (mMediaRecorder != null) {
            try {
                mMediaRecorder.setOnErrorListener(null);
                mMediaRecorder.setOnInfoListener(null);
                mMediaRecorder.setPreviewDisplay(null);
                mMediaRecorder.stop();
            } catch (IllegalStateException e) {
                MyLog.d(MyLog.TAG, "stopRecord"+ e);
            } catch (RuntimeException e) {
                MyLog.d(MyLog.TAG, "stopRecord"+ e);
            } catch (Exception e) {
                MyLog.d(MyLog.TAG, "stopRecord"+ e);
            }
            mMediaRecorder.release();
            mMediaRecorder = null;

        }
    }


}
